﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OF.Component.BLL;

using OF.Component.Model;
using OF.Component.Base;
using OF.Utility;

namespace _2010web.Manage.authority
{
    public partial class AuthorityRoleMenuRelaEdit : OpBasePage
    {
        protected int intRoleID = 0;
        protected int intSystemID = 0;
        protected string strMessage = "";
        protected string strMenuJosn = "";
        protected string strHtml = "";
        protected string strMenu = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindMenuList();

            }
        }

        private void BindMenuList()
        {
            int count = 0;
            intSystemID = RequestUtility.GetQueryInt("sysid", 1);
            intRoleID = RequestUtility.GetQueryInt("id", 1);
            string strMenu = AuthorityRoleMenuRelaOperate.GetAuthorityToString(intRoleID, intSystemID);
            IList<AuthorityMenuInfo> list = AuthorityMenuOperate.Search(out count, 1, 9999, "1=1 and IsActive=1 and SystemID=" + intSystemID, "OrderNum", false);

            var ParentList = from AuthorityMenu in list
                             where AuthorityMenu.ParentID == 0
                             select new { MenuName = AuthorityMenu.MenuName, MenuID = AuthorityMenu.MenuID, MenuUrl = AuthorityMenu.MenuUrl };

            StringBuilder sb = new StringBuilder();

            foreach (var pv in ParentList)
            {
                sb.AppendLine("<dl>");
                sb.AppendFormat(" <dt><em> <input type=\"checkbox\" value=\"{0}\" {2} class=\"ckb\" level=\"1\" name=\"radlst\">{1}</em> </dt>", pv.MenuID, pv.MenuName, checkAuthority(strMenu, pv.MenuID));
                sb.AppendLine(" <dd><div class=\"checkbox_grp\"><span>");

                var ChildList = from AuthorityMenu in list
                                where AuthorityMenu.ParentID == pv.MenuID
                                select new { MenuName = AuthorityMenu.MenuName, MenuID = AuthorityMenu.MenuID, MenuUrl = AuthorityMenu.MenuUrl };

                foreach (var cv in ChildList)
                {
                    sb.AppendFormat("<input type=\"checkbox\"  class=\"ckb\" value=\"{0}\"  level=\"2\" {2}  ><label>{1}</label>", cv.MenuID, cv.MenuName, checkAuthority(strMenu,cv.MenuID));

                }
                sb.AppendLine("</span></div></dd>");
                sb.AppendLine("</dl>");


            }

            strHtml = sb.ToString();

        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string MessageInfo = "信息修改成功！";
            intSystemID = RequestUtility.GetQueryInt("sysid", -1);
            //intID = RequestUtility.GetQueryInt("ID", -1);

            strMessage = MessageDivInfo(MessageInfo, "");
        }

        private string checkAuthority(string strMenu, int MenuID)
        {
            string strRun = "";
            if (strMenu.IndexOf("," + MenuID.ToString() + ",") > -1)
            {
                strRun = "checked=\"true\"";
            }

            return strRun;
        }
    }
}
